<template>
  <div>
    <el-button type="primary" plain @click="dialogFormVisible = true">添加书籍</el-button>

    <el-table :data="books" style="width: 100%">
      <el-table-column label="编号" prop="id" />
      <el-table-column label="书名" prop="bookname" />
      <el-table-column label="作者" prop="name" />
      <el-table-column label="价格" prop="price" />
      <el-table-column label="出版社" prop="publishe" />
      <el-table-column align="right">
        <template #default="scope">
          <el-button size="small" @click="editBook(scope.row)">更新</el-button>
          <el-button size="small" type="danger" @click="deleteBook(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加/更新书籍的对话框 -->
    <el-dialog v-model="dialogFormVisible" title="书籍信息" width="500" @close="book = {}">
      <el-form :model="book">
        <el-form-item label="书名">
          <el-input v-model="book.bookname" autocomplete="off" />
        </el-form-item>
        <el-form-item label="作者">
          <el-input v-model="book.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="价格">
          <el-input v-model="book.price" autocomplete="off" />
        </el-form-item>
        <el-form-item label="出版社">
          <el-input v-model="book.publishe" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="saveBook">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { ElMessage, ElMessageBox } from "element-plus";

export default {
  name: "BookView",
  data() {
    return {
      books: [],
      keyword: "",
      dialogFormVisible: false,
      book: {},
    };
  },
  methods: {
    loadBooks() {
      this.$axios
          .get("/findBook")
          .then((result) => {
            if (result.data.code === 200) {
              this.books = result.data.data;
            }
          })
          .catch((error) => {
            console.error("加载书籍时出错:", error);
          });
    },

    // 编辑书籍
    editBook(book) {
      this.dialogFormVisible = true;
      this.book = JSON.parse(JSON.stringify(book));
    },

    // 添加数据
    saveBook() {
      if (this.book.id) {
        this.$axios
            .post("/updateBook", this.$qs.stringify(this.book))
            .then((result) => {
              if (result.data.code === 200) {
                ElMessage.success("更新成功");
                this.loadBooks();
              }
            })
            .catch((error) => {
              console.error("更新书籍时出错:", error);
            });
      } else {
        // 否则是添加操作
        this.$axios
            .post("/addBook", this.$qs.stringify(this.book))
            .then((result) => {
              if (result.data.code === 200) {
                ElMessage.success("添加成功");
                this.loadBooks();
              }
            })
            .catch((error) => {
              console.error("添加书籍时出错:", error);
            });
      }
      this.dialogFormVisible = false;
    },

    // 删除书籍
    deleteBook(id) {
      ElMessageBox.confirm("是否确定删除？", "警告", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      })
          .then(() => {
            this.$axios
                .post("/deleteBook?id=" + id)
                .then((result) => {
                  if (result.data.code === 200) {
                    ElMessage.success("删除成功");
                    this.loadBooks();
                  }
                })
                .catch((error) => {
                  console.error("删除书籍时出错:", error);
                });
          })
          .catch(() => {
          });
    },
  },
  mounted() {
    this.loadBooks();
  },
};
</script>

<style scoped>
.dialog-footer {
  text-align: right;
}
</style>